package com.wnxy.admin.task;

import com.wnxy.admin.entity.Region;
import com.wnxy.admin.service.IRegionService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class LoadChinaAddressTask implements CommandLineRunner {

    private final IRegionService regionServiceImpl;
    private final RedisTemplate redisTemplate;

    public LoadChinaAddressTask(IRegionService regionServiceImpl, RedisTemplate redisTemplate) {
        this.regionServiceImpl = regionServiceImpl;
        this.redisTemplate = redisTemplate;
    }

    //项目启动时，执行
    @Override
    public void run(String... args) throws Exception {
        List<Region> list = regionServiceImpl.list();
        list.forEach(e->{
            redisTemplate.opsForHash().put("china",e.getId()+"",e.getName());
        });
    }
}
